package com.yiqi.lottery.feature.find.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.framework.AM;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.entity.bo.CircleBo;
import com.yiqi.lottery.feature.find.view.ImageActivity;
import com.yiqi.lottery.framework.DataServer;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CircleAdapter extends BaseQuickAdapter<CircleBo, BaseViewHolder> {
    private GetCheckedValue getCheckedValue;

    // 存储勾选框状态的map集合  解决滑动的复用问题
    private Map<Integer, Boolean> map = new HashMap<>();
    Context mContext;

    public CircleAdapter(Context context) {
        super(R.layout.item_find_circle);
        this.mContext = context;

    }

    @Override
    protected void convert(BaseViewHolder helper, CircleBo item) {
        helper.setText(R.id.tv_find_circle_list_name, item.getUser_info().getUsername() + "")
                .setText(R.id.tv_find_circle_list_date, DateTool.TimeStamp2Date(item.getCreated_at() + ""))
                .setText(R.id.tv_find_circle_list_content, item.getPost_content().replaceAll("\\s*", ""))
                .setText(R.id.cbx_find_circle_like, item.getLike_num() + "");
        AM.image().bindToCircleObject(item.getUser_info().getAvatar(), helper.getView(R.id.iv_find_circle_list_head));
        CheckBox checkBox = helper.getView(R.id.cbx_find_circle_like);
        if (item.getIs_like() == 0) {
            checkBox.setChecked(false);
            checkBox.setButtonDrawable(R.drawable.icon_nolike);
        } else {
            checkBox.setChecked(true);
            checkBox.setButtonDrawable(R.drawable.icon_like);
        }


        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(!compoundButton.isPressed())return;

                int num= Integer.parseInt(compoundButton.getText().toString());
                if (item.getIs_like() == 0) {
                    map.put(helper.getAdapterPosition(), true);
                    getCheckedValue.getValue(true, item.getPost_id(), helper.getAdapterPosition());
                    item.setIs_like(1);
                    compoundButton.setText((num + 1) + "");
                    checkBox.setButtonDrawable(R.drawable.icon_like);
                    checkBox.setTextColor(AM.res().getColor(R.color.primary_highlight));
                } else {
                    map.put(helper.getAdapterPosition(), false);
                    getCheckedValue.getValue(false, item.getPost_id(), helper.getAdapterPosition());
                    item.setIs_like(0);
                    if (num> 0) {
                        compoundButton.setText((num - 1) + "");
                        checkBox.setButtonDrawable(R.drawable.icon_nolike);
                        checkBox.setTextColor(AM.res().getColor(R.color.text_primary));
                    }
                }


            }
        });
        RecyclerView rvImg = helper.getView(R.id.rv_find_circle_list_img);
        rvImg.setLayoutManager(new GridLayoutManager(AM.app(), 4));
        ImgAdapter imgAdapter = new ImgAdapter(item.getPost_files());
        rvImg.setAdapter(imgAdapter);

        if (map.get(helper.getAdapterPosition()) == null) {
            map.put(helper.getAdapterPosition(), false);
        }

        checkBox.setChecked(map.get(helper.getAdapterPosition()));

        imgAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, ImageActivity.class);
                intent.putExtra("images", (Serializable) item.getPost_files());
                intent.putExtra("pos", position);
                mContext.startActivity(intent);
            }
        });

    }


    class ImgAdapter extends BaseQuickAdapter<CircleBo.PostFilesBean, BaseViewHolder> {

        public ImgAdapter(@Nullable List<CircleBo.PostFilesBean> data) {
            super(R.layout.item_find_circle_img, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CircleBo.PostFilesBean item) {
            AM.image().bind(item.getFile_url(), helper.getView(R.id.iv_find_circle_img));
        }
    }

    public interface GetCheckedValue {
        void getValue(boolean stauts, Integer ids, int pos);
    }

    public void setOnItemChecked(GetCheckedValue getCheckedValue) {
        this.getCheckedValue = getCheckedValue;
    }

}
